<template>
  <el-container>
    <el-main>
      <div id="container"></div>
      <div id="panel"></div>
    </el-main>
  </el-container>
</template>

<script>
import transportLineType from '@/api/base/transportLineType/index'
import transportLine from '@/api/base/transportLine/index'
import coreOrg from '@/api/base/coreOrg/index'
import AMap from 'AMap'

export default {
  name: 'index',
  props: ['startLng', 'startLat', 'endLng', 'endLat'],
  methods: {
    showLine(row) {
      this.dialogVisible = true
      let startAgencyId = row.startAgencyId
      let endAgencyId = row.endAgencyId
      console.log('执行中!')
      /**
       * 起点机构
       */
      coreOrg.findById(endAgencyId).then(data => {
        this.endLng = data.data.longitude
        this.endLat = data.data.latitude
      }).catch(e => {
        alert('错误信息为:' + e)
      })
      /***
       * 终点机构
       */
      coreOrg.findById(startAgencyId).then(data => {
        this.startLng = data.data.longitude
        this.startLat = data.data.latitude
      }).catch(e => {
        alert('错误信息为:' + e)
      })
      setTimeout(() => {
        this.showMap()
      }, 500)
    },
    //高德地图
    showMap() {
      this.map = new AMap.Map('container', {
        center: [116.397559, 39.89621],
        zoom: 14
      })
      this.drivingOption = {
        policy: AMap.DrivingPolicy.LEAST_TIME, // 其它policy参数请参考 https://lbs.amap.com/api/javascript-api/reference/route-search#m_DrivingPolicy
        ferry: 1, // 是否可以使用轮渡
        province: '京', // 车牌省份的汉字缩写
        map: this.map,
        panel: 'panel'
      }
      if (this.driving) {
        this.driving.clear()
      }
      this.driving = new AMap.Driving(this.drivingOption)
      // 根据起终点经纬度规划驾车导航路线
      this.driving.search(new AMap.LngLat(this.startLng, this.startLat), new AMap.LngLat(this.endLng, this.endLat), this.amapStaus)
    },
    amapStaus(status, result) {
      // result 即是对应的驾车导航信息，相关数据结构文档请参考  https://lbs.amap.com/api/javascript-api/reference/route-search#m_DrivingResult
      if (status === 'complete') {
        console.log('', result)
        console.log('绘制驾车路线完成')
      } else {
        console.log('获取驾车数据失败：' + result)
      }
    }
  }
}
</script>

<style scoped>
html,
body,
#container {
  width: 100%;
  height: 100%;
}

#panel {
  position: fixed;
  background-color: white;
  max-height: 90%;
  overflow-y: auto;
  top: 10px;
  right: 400px;
  width: 280px;
}

#panel .amap-call {
  background-color: #009cf9;
  border-top-left-radius: 4px;
  border-top-right-radius: 4px;
}

#panel .amap-lib-driving {
  border-bottom-left-radius: 4px;
  border-bottom-right-radius: 4px;
  overflow: hidden;
}
</style>
